package com.jb.repair.dao;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.jb.repair.F1Application;
import com.jb.repair.acticity.CheckLocActivity;
import com.jb.repair.common.AppConfig;
import com.jb.repair.common.Constants;
import com.jb.repair.model.FileLbModel;
import com.jb.repair.model.RegularWorkModel;
import com.jb.repair.model.ResultModel;
import com.jb.repair.model.ResultStatisticModel;
import com.jb.repair.util.TimeUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by gaobin on 2015/11/7.
 */
public class ModifyDb {
    private static String TAG = "ModifyDb";
    private static SQLiteDatabase db;
    private static ModifyDb mydb;
    private LocalDbDao dao;

    private static final String TASK_RESULT_TABLE_NAME = "TB_SI_TASK_RESULT";    //点检任务结果表表名
    private static final String ABNORMAL_TABLE_NAME = "TB_EQ_ABNORMAL";       //异常管理表表名
    private static final String DEFECT_TABLE_NAME = "TB_EQ_DEFECT";           //缺陷管理表表名

    private String userName;
    private String userId;
    SharedPreferences sp;

    private ModifyDb(Context context) {
        dao = LocalDbDao.getInstance(context);
        db = context.getApplicationContext().openOrCreateDatabase(AppConfig.DB_PATh, Context.MODE_PRIVATE, null);
        sp = F1Application.getAppConext().getSharedPreferences(Constants.SP_NAME, Context.MODE_PRIVATE);
    }

    public static ModifyDb getInstance(Context context) {
        if (mydb == null)
            mydb = new ModifyDb(context);
        return mydb;
    }

    private void closeDb() {
        db.close();
        mydb = null;
    }

    /**
     * 登记观察类点检结果
     *
     * @return
     */
    public Boolean regist(ResultModel model) {
        userId = sp.getString(Constants.PERSON_GUID, "");
        userName = sp.getString(Constants.PERSON_NAME, "");
        ContentValues cv = new ContentValues();
        cv.put("is_abnormal", model.is_abnormal);
        cv.put("is_defect", model.is_defect);
        cv.put("si_person_id", F1Application.getUserId());
        cv.put("si_person", F1Application.getUserName());
        cv.put("si_time", TimeUtil.getSystemTime());
        cv.put("is_forgot", "F");
        cv.put("abnormal_desc", model.abnormal_desc);
        cv.put("defect_desc", model.defect_desc);
        cv.put("defect_level_id", model.defect_level_id);
        cv.put("defect_type_guid", model.defect_type_id);
        cv.put("attachment", model.attachment);
        cv.put("task_id", CheckLocActivity.task_id);
        cv.put("is_must_today", model.is_must_today);
        db.update(TASK_RESULT_TABLE_NAME, cv, "GUID=?", new String[]{model.GUID});
        dao.insertResult(model);
        if (model.is_abnormal.trim().equals("T")) {
            ContentValues abnormalCv = new ContentValues();
            abnormalCv.put("guid", model.abnormal_id);
            abnormalCv.put("found_time", TimeUtil.getSystemTime());
            abnormalCv.put("creat_time", TimeUtil.getSystemTime());
            abnormalCv.put("eq_id", model.equip_id);
            abnormalCv.put("abn_describe", model.abnormal_desc);
            abnormalCv.put("source_id", "0303104");
            abnormalCv.put("creat_user_id", userId);
            abnormalCv.put("foundman_id", userId);
            abnormalCv.put("inspect_id", model.GUID);
            abnormalCv.put("attachment", model.attachment);
//            abnormalCv.put("is_must_today", model.is_must_today);
            db.insert(ABNORMAL_TABLE_NAME, null, abnormalCv);
            dao.insertAbnormal(abnormalCv);
        } else if (model.is_defect.trim().equals("T")) {
            ContentValues abnormalCv = new ContentValues();
            abnormalCv.put("guid", model.defect_id);
            abnormalCv.put("inspect_id", model.GUID);
            abnormalCv.put("attachment", model.attachment);
            abnormalCv.put("found_time", TimeUtil.getSystemTime());
            abnormalCv.put("creat_time", TimeUtil.getSystemTime());
            abnormalCv.put("eq_id", model.equip_id);
            abnormalCv.put("defect_describe", model.defect_desc);
            //  abnormalCv.put("defect_report", model.attachment);
            abnormalCv.put("type_id", model.defect_type_id);
            abnormalCv.put("source_id", "0303203");
            abnormalCv.put("level_id", model.defect_level_id);
            abnormalCv.put("foundman_id", userId);
            abnormalCv.put("creat_user_id", userId);
//            abnormalCv.put("is_must_today", model.is_must_today);
            db.insert(DEFECT_TABLE_NAME, null, abnormalCv);
            dao.insertDefect(abnormalCv);
        }
        closeDb();
        return true;
    }

    /**
     * 登记异常
     * @param model
     * @return
     */
    public Boolean registAbnormal(ResultModel model) {
        userId = sp.getString(Constants.PERSON_GUID, "");
        userName = sp.getString(Constants.PERSON_NAME, "");
        ContentValues cv = new ContentValues();
        cv.put("is_abnormal", "T");
        cv.put("is_defect", "F");
        cv.put("si_person_id", F1Application.getUserId());
        cv.put("si_person", F1Application.getUserName());
        cv.put("si_time", TimeUtil.getSystemTime());
        cv.put("is_forgot", "F");
        cv.put("abnormal_desc", model.abnormal_desc);
        cv.put("defect_desc", model.defect_desc);
        cv.put("defect_level_id", model.defect_level_id);
        cv.put("defect_type_guid", model.defect_type_id);
        cv.put("task_id", CheckLocActivity.task_id);
        cv.put("is_must_today", model.is_must_today);
        db.update(TASK_RESULT_TABLE_NAME, cv, "GUID=?", new String[]{model.GUID});
        dao.insertResult(model);
        closeDb();
        return true;
    }

    /**
     * 撤销点检结果
     *
     * @param resuidId 结果id
     */
    public void revokeRegis(String resuidId) {
        ContentValues cv = new ContentValues();
        cv.put("is_abnormal", "");
        cv.put("is_defect", "");
        cv.put("si_person_id", "");
        cv.put("si_person", "");
        cv.put("si_time", "");
        cv.put("is_forgot", "");
        cv.put("abnormal_desc", "");
        cv.put("defect_desc", "");
        cv.put("defect_level_id", "");
        cv.put("defect_type_guid", "");
        dao.delectResultById(resuidId);
        db.update(TASK_RESULT_TABLE_NAME, cv, "GUID=?", new String[]{resuidId});
    }

    /**
     * 登记数值类点检结果
     *
     * @param model
     */
    public void regisNum(ResultModel model) {
        userId = sp.getString(Constants.PERSON_GUID, "");
        userName = sp.getString(Constants.PERSON_NAME, "");
        ContentValues cv = new ContentValues();
        cv.put("is_abnormal", model.is_abnormal);
        cv.put("is_defect", "F");
        cv.put("si_person_id", userId);
        cv.put("si_person", userName);
        cv.put("si_time", TimeUtil.getSystemTime());
        cv.put("is_forgot", "F");
        cv.put("num_result", model.num_result);
        cv.put("is_must_today", model.is_must_today);

        dao.insertResult(model);
        db.update(TASK_RESULT_TABLE_NAME, cv, "GUID=?", new String[]{model.GUID});
    }

    /**
     * 批量登记指定位置下所有项目
     */
    public void batchCheck(String placeId, List<String> guidList) {
        userId = sp.getString(Constants.PERSON_GUID, "");
        userName = sp.getString(Constants.PERSON_NAME, "");
        String method;
        method = "0401901";
        String querySql = "SELECT GUID FROM tb_si_task_result where " +
                "TASK_ID='" + CheckLocActivity.task_id + "' " +
                "and place_id='" + placeId + "' " +
                "and si_person_id='" + "' " +
                "and method='" + method + "'";

        Cursor c = db.rawQuery(querySql, null);
        while (c.moveToNext()) {
            String resultId = c.getString(0);
            ContentValues cv = new ContentValues();
            cv.put("is_abnormal", "F");
            cv.put("is_defect", "F");
            cv.put("si_person_id", userId);
            cv.put("si_person", userName);
            cv.put("si_time", TimeUtil.getSystemTime());
            cv.put("is_forgot", "F");
            ResultModel model = new ResultModel();
            model.GUID = resultId;
            model.is_abnormal = "F";
            model.is_defect = "F";
            model.is_forgot = "F";
            dao.insertResult(model);
            db.update(TASK_RESULT_TABLE_NAME, cv, "GUID=?", new String[]{resultId});
        }
        for (int a = 0; a < guidList.size(); a++) {
            revokeRegis(guidList.get(a));
        }
    }

    public void setAllTaskStatus(String status) {
        ContentValues cv = new ContentValues();
        cv.put("task_status", status);
        db.update("TB_SI_TASK", cv, null, null);
    }

    public void setTaskStatus(String taskId, String status) {
        ContentValues cv = new ContentValues();
        cv.put("task_status", status);
        db.update("TB_SI_TASK", cv, "GUID=?", new String[]{taskId});
    }

    public void insertFile(FileLbModel model) {
        if (model != null) {
            Log.i("main","C");
            String sql = "select * from TB_FILE_LB where GUID='" + model.guid + "'";
            Cursor c = db.rawQuery(sql, null);
            if (c.moveToFirst()) {
                dao.insertFile(model);
                c.close();
                return;
            }else {
                dao.insertFile(model);
            }
            c.close();
            Log.i("main","D");
            ContentValues cv = new ContentValues();
            cv.put("GUID", model.guid);
            cv.put("WJMC", model.wjmc);
            cv.put("LX", model.lx);
            cv.put("TIMEPOINT", model.TIMEPOINT);
            db.insert("TB_FILE_LB", null, cv);
            Log.i("main", "图片插入成功");
        }
    }

    public void modifyResult(String resultId, String explain, Boolean isAbnormal) {
        ContentValues cv = new ContentValues();
        if (isAbnormal) {
            cv.put("abnormal_desc", explain);
        } else
            cv.put("defect_desc", explain);
        db.update("TB_SI_TASK_RESULT", cv, "GUID=?", new String[]{resultId});
        dao.modifyResult(resultId, cv);
    }

    public void delectByResultId(String resultId) {
        db.delete("TB_EQ_ABNORMAL", "inspect_id=?", new String[]{resultId});
        db.delete("TB_EQ_DEFECT", "inspect_id=?", new String[]{resultId});
        dao.delectByResultId(resultId);
    }


}
